package com.usian.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
        //判断channelDto是否为null
        if(channelDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查分页参数
        channelDto.checkParam();
        //根据套件来进行查询
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        //不为null是 增加条件
        queryWrapper.like(channelDto.getName()!=null, AdChannel::getName,channelDto.getName());
        //分页查询
        //封装page对象
        Page pageInfo = new Page(channelDto.getPage(),channelDto.getSize());
        //调用分页方法
        IPage<AdChannel> page = page(pageInfo,queryWrapper);
        //结果封装
        ResponseResult responseResult = new PageResponseResult(channelDto.getPage(),channelDto.getSize(), (int) page.getTotal());
        //封装数据
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult saveChannel(AdChannel channel) {
        //判定参数是否存在
        if(channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置创建时间的参数
        channel.setCreatedTime(new Date());
        //保存
        save(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdChannel channel) {
        //判定参数是否存在
        if(channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改
        updateById(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //判断id是否存在
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否存在  状态是否有效
        AdChannel adChannel = getById(id);
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道有效不能删除");
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
